package zhuyun.code.launcheractivity.view

import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.Toast
import com.google.gson.Gson
import com.king.zxing.util.CodeUtils
import kotlinx.android.synthetic.main.activity_new_qr_code.*
import kotlinx.android.synthetic.main.activity_new_qr_code.view.*
import kotlinx.android.synthetic.main.popup_qrcode.view.*
import zhuyun.code.launcheractivity.R
import zhuyun.code.launcheractivity.model.Constants
import zhuyun.code.launcheractivity.model.NetworkRequest
import zhuyun.code.launcheractivity.model.ResponseDataHandler
import zhuyun.code.launcheractivity.model.data.BaseResponse
import zhuyun.code.launcheractivity.model.data.FriendDetailsBean
import zhuyun.code.launcheractivity.model.data.QRCodeBean
import zhuyun.code.launcheractivity.utils.CommonUtils
import zhuyun.code.launcheractivity.utils.GlideUtil

class NewQRCodeActivity : BaseActivity(),View.OnClickListener {
    private val mPopupWindow by lazy {
        val pop = LayoutInflater.from(this).inflate(R.layout.popup_qrcode,rl_code,false)
        val popupWindow = PopupWindow(pop,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT,false)
        popupWindow.animationStyle = R.style.mypopwindow_anim_style
        popupWindow.isOutsideTouchable = true
        popupWindow.setOnDismissListener { backgroundAlpha(1.0f) }
        pop.tv_save.setOnClickListener {
            CommonUtils.saveImageToGallery(this,getBitmap(rl_code))
            Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show()
        }
        pop.tv_cancel.setOnClickListener{
            popupWindow.dismiss()
        }
        popupWindow
    }

    private fun getBitmap(view: View):Bitmap{
        val screenView = window.decorView
        screenView.isDrawingCacheEnabled = true
        screenView.buildDrawingCache()
        var bitmap = screenView.drawingCache
        if(bitmap != null){
            val outWidth = view.width
            val outHeight = view.height
            val viewLocationArray = IntArray(2)
            view.getLocationOnScreen(viewLocationArray)
            bitmap = Bitmap.createBitmap(bitmap,viewLocationArray[0],viewLocationArray[1],outWidth,outHeight)
        }
        return bitmap
    }

    private var type:Int = 0
    private lateinit var id:String



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_qr_code)
        type = intent.getIntExtra(Constants.POST_TYPE,1)
        id = intent.getStringExtra(Constants.ID)!!
        if(type == 1){//个人
            NetworkRequest.getFriendDetails(id,object :ResponseDataHandler<FriendDetailsBean>(){
                override fun onSuccess(
                    resultData: FriendDetailsBean?,
                    baseResponse: BaseResponse<FriendDetailsBean>
                ) {
                    resultData?.let {
                        GlideUtil.displayEspImage(it.HeadUrl,iv_head,R.drawable.pic_touxiang_normal)
                        tv_name.text = it.Remarks
                        tv_area.text = it.CountryName
                        when(it.Sex){
                            0->{
                                iv_sex.setImageResource(R.drawable.icon_female_data)
                            }
                            1->{
                                iv_sex.setImageResource(R.drawable.icon_male_data)
                            }

                        }

                    }

                }

                override fun onFailure(errMsg: String?) {
                    Toast.makeText(this@NewQRCodeActivity,errMsg,Toast.LENGTH_SHORT).show()
                }

            })
        }
        val myQRBean = QRCodeBean("$type",id)
        val result = Gson().toJson(myQRBean)
        val qrCode = CodeUtils.createQRCode(result,600)
        iv.setImageBitmap(qrCode)
        iv_back.setOnClickListener(this)
        iv_more.setOnClickListener(this)


    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.iv_back->{
                finish()
            }
            R.id.iv_more->{
                mPopupWindow.showAtLocation(rl_code,Gravity.BOTTOM,0,0)
                backgroundAlpha(0.7f)
            }


        }

    }
}
